package net.sk.china.city.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.sk.china.common.exception.ServiceException;
import net.sk.china.city.utils.PlatformIdHolder;
import net.sk.china.city.service.IArArticleCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import net.sk.china.city.mapper.ArArticleCategoryMapper;
import net.sk.china.city.domain.ArArticleCategory;

/**
 * 文章类别Service业务层处理
 * 
 * @author gxf
 * @date 2024-03-26
 */
@Service
public class ArArticleCategoryServiceImpl implements IArArticleCategoryService
{
    @Autowired
    private ArArticleCategoryMapper arArticleCategoryMapper;

    /**
     * 查询文章类别
     * 
     * @param id 文章类别主键
     * @return 文章类别
     */
    @Override
    public ArArticleCategory selectArArticleCategoryById(Long id)
    {
        return arArticleCategoryMapper.selectArArticleCategoryById(id);
    }

    /**
     * 查询文章类别列表
     * 
     * @param arArticleCategory 文章类别
     * @return 文章类别
     */
    @Override
    public List<ArArticleCategory> selectArArticleCategoryList(ArArticleCategory arArticleCategory)
    {
        arArticleCategory.setPlatformId(PlatformIdHolder.getPlatformId());
        return arArticleCategoryMapper.selectArArticleCategoryList(arArticleCategory);
    }

    /**
     * 新增文章类别
     * 
     * @param arArticleCategory 文章类别
     * @return 结果
     */
    @Override
    public int insertArArticleCategory(ArArticleCategory arArticleCategory)
    {
        ArArticleCategory category = arArticleCategoryMapper.selectOne(new QueryWrapper<ArArticleCategory>()
                .eq("cate_name", arArticleCategory.getCateName())
                .like("platform_id","%" + PlatformIdHolder.getPlatformId() +"%"));
        if(category != null){
            throw new ServiceException("该类别名已存在");
        }
        arArticleCategory.setPlatformId(PlatformIdHolder.getPlatformId());
        return arArticleCategoryMapper.insertArArticleCategory(arArticleCategory);
    }

    /**
     * 修改文章类别
     * 
     * @param arArticleCategory 文章类别
     * @return 结果
     */
    @Override
    public int updateArArticleCategory(ArArticleCategory arArticleCategory) {
        String newCateName = arArticleCategory.getCateName();
        ArArticleCategory category = arArticleCategoryMapper.selectOne(new QueryWrapper<ArArticleCategory>()
                .eq("id", arArticleCategory.getId()));
        if (category != null && newCateName != null && !category.getCateName().equals(newCateName)) {
            ArArticleCategory articleCategory = arArticleCategoryMapper.selectOne(new QueryWrapper<ArArticleCategory>()
                    .ne("id",arArticleCategory.getId())
                    .eq("cate_name",arArticleCategory.getCateName())
                    .like("platform_id","%" + PlatformIdHolder.getPlatformId() +"%"));
            if(articleCategory != null ){
                throw new ServiceException("该类别名已存在");
            }
        }
        return arArticleCategoryMapper.updateArArticleCategory(arArticleCategory);
    }

    /**
     * 批量删除文章类别
     * 
     * @param ids 需要删除的文章类别主键
     * @return 结果
     */
    @Override
    public int deleteArArticleCategoryByIds(Long[] ids)
    {
        return arArticleCategoryMapper.deleteArArticleCategoryByIds(ids);
    }

    /**
     * 删除文章类别信息
     * 
     * @param id 文章类别主键
     * @return 结果
     */
    @Override
    public int deleteArArticleCategoryById(Long id)
    {
        return arArticleCategoryMapper.deleteArArticleCategoryById(id);
    }


    /**
     * 获取所有的类别名和id
     */
    @Override
    public List<ArArticleCategory> getNameAndId(){
        return arArticleCategoryMapper.selectList(new QueryWrapper<ArArticleCategory>()
                .like("platform_id", "%" + PlatformIdHolder.getPlatformId() + "%"));
    }


    /**
     * 修改类别状态
     * @param arArticleCategory
     * @return
     */
    @Override
    public int updateStatus(ArArticleCategory arArticleCategory){
        return arArticleCategoryMapper.updateArArticleCategory(arArticleCategory);
    }
}
